//
//  NSObject+MYKVC.m
//  KVC的实现
//
//  Created by webplus on 17/8/31.
//  Copyright © 2017年 sanyi. All rights reserved.
//

#import "NSObject+MYKVC.h"
#import <objc/runtime.h>

@implementation NSObject (MYKVC)


- (void)setMyValue:(id)value forKey:(NSString *)key{
    if (key == nil ||key.length == 0) {
        return;
    }
    if ([value isKindOfClass:[NSNull class]]) {
        [self setNilValueForKey:key];
        return;
    }
    if (value == nil) {
        [self setNilValueForKey:key];
        return;
    }
    if (![value isKindOfClass:[NSObject class]]) {
        @throw @"must be a NSObject type";
        return;
    }
    
    NSString *funcName = [NSString stringWithFormat:@"set%@:",key.capitalizedString];
    //如果有set方法，调用set方法
    if ([self respondsToSelector:NSSelectorFromString(funcName)]) {
        [self performSelector:NSSelectorFromString(funcName) withObject:value];
        return;
    }
    //不能合成set方法，手动合成
    unsigned int count;
    BOOL flag = false;
    //遍历属性
    Ivar* vars = class_copyIvarList([self class], &count);
    for (NSInteger i = 0; i<count; i++) {
        Ivar var = vars[i];
        NSString *keyName = [[NSString stringWithCString:ivar_getName(var) encoding:NSUTF8StringEncoding] substringFromIndex:1];
        
        NSString *keyName1 = [NSString stringWithCString:ivar_getName(var) encoding:NSUTF8StringEncoding];
        NSLog(@"%@",keyName1);
        //两种方法合成实例变量和属性__key与_key
        if ([keyName isEqualToString:[NSString stringWithFormat:@"_%@",key]]) {
            flag = true;
            //赋值
            object_setIvar(self, var, value);
            break;
        }
        
        if ([keyName isEqualToString:key]) {
            flag = true;
            object_setIvar(self, var, value);
            break;
        }
        
    }
    if (!flag) {
        [self setValue:value forUndefinedKey:key];//如果需要完全自定义，那么这里需要写一个self setMyValue:value forUndefinedKey:key，但是必要性不是很大，就省略了
    }
    
}

-(id)myValueforKey:(NSString *)key{
    if (key == nil || key.length == 0) {
        return [NSNull new]; //其实不能这么写的
    }
    //这里为了更方便，我就不做相关集合的方法查询了
    NSString* funcName = [NSString stringWithFormat:@"gett%@:",key.capitalizedString];
    if ([self respondsToSelector:NSSelectorFromString(funcName)]) {
        return [self performSelector:NSSelectorFromString(funcName)];
    }
    unsigned int count;
    BOOL flag = false;
    Ivar* vars = class_copyIvarList([self class], &count);
    for (NSInteger i = 0; i<count; i++) {
        Ivar var = vars[i];
        NSString* keyName = [[NSString stringWithCString:ivar_getName(var) encoding:NSUTF8StringEncoding] substringFromIndex:1];
        if ([keyName isEqualToString:[NSString stringWithFormat:@"_%@",key]]) {
            flag = true;
            return     object_getIvar(self, var);
            break;
        }
        if ([keyName isEqualToString:key]) {
            flag = true;
            return     object_getIvar(self, var);
            break;
        }
    }
    if (!flag) {
        [self valueForUndefinedKey:key];//如果需要完全自定义，那么这里需要写一个self myValueForUndefinedKey，但是必要性不是很大，就省略了
    }
    return [NSNull new];
}

- (void)setNilValueForKey:(NSString *)key{
    NSLog(@"出现异常，%@的值不存在",key);
}

@end
